package com.hmall.controller;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.service.*;
import com.hmall.mapper.SysUserMapper;
import com.hmall.vo.BigList;
import com.hmall.vo.PageVo;
import com.hmall.vo.ShortCompete;
import com.itextpdf.text.DocumentException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;


/**
 * @Author zhoutao
 * @Date 2018/11/8 10:43
 * @Param
 * @return
 * @Description //
 */
@RestController
@RequestMapping("/back/")
@Api(value = "后台使用的接口")
public class BackController {


    private ProductClassService iProductClassService;

    private BackService backService;

    private SysUserMapper userMapper;

    private DealerService dealerService;

    private MerchandiserService merchandiserService;

    private final AuctionService auctionService;

    @Autowired
    public BackController(ProductClassService iProductClassService, BackService backService, SysUserMapper userMapper, DealerService dealerService, MerchandiserService merchandiserService, AuctionService auctionService) {
        this.iProductClassService = iProductClassService;
        this.backService = backService;
        this.userMapper = userMapper;
        this.dealerService = dealerService;
        this.merchandiserService = merchandiserService;
        this.auctionService = auctionService;
    }

    /**
     * @Description // 得到筛选项
     * @Date 2018/11/2 9:48
     */
    @PostMapping("filter")
    @ApiOperation(value = "筛选列表")
    public ServerResponse<BigList> listBigList(HttpServletRequest request, String typeName, @RequestParam(value = "shopId", defaultValue = "1") Integer shopId) {

        return iProductClassService.listBigList(request, "".equals(typeName) ? null : typeName, shopId, 2);

    }


    @PostMapping("compete")
    @ApiOperation(value = "具体产品", notes = "typeName: 产品类型|fineAndPoreName:纤度孔数|glossName:光泽|gradeName:等级|singleShareName:单合股|sectionName:截面|colorName:颜色|twistName:捻向|networkName:网络")
    public ServerResponse<PageVo<ShortCompete>> listComproduct(Integer userId, String typeName, String fineAndPoreName, String glossName, String gradeName, String singleShareName,
                                                               String sectionName, String colorName, String twistName, String networkName,
                                                               @RequestParam(value = "shopId", defaultValue = "1") int shopId,
                                                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {


        return iProductClassService.listComProduct(userMapper.selectByPrimaryKey(userId), typeName, fineAndPoreName, glossName, gradeName, singleShareName, sectionName, colorName, twistName, networkName, shopId, pageNum, pageSize, 2);
    }

    /**
     * @Description // 生成对账单
     * @Date 2018/11/2 9:49
     */
    @PostMapping("createOrderFuillmentPDF")
    @ApiOperation("生成对账单pdf")
    public ServerResponse createOrderFufillmentPDF(String mallOrderFufillmentIdStr, Integer customerId, String startTime, String endTime) throws FileNotFoundException, DocumentException {

        return backService.createOrderFufillmentPDF(mallOrderFufillmentIdStr, customerId, startTime, endTime);
    }


    /***
     * @Description // 后台根据计划创建订单
     * @Date 2018/11/2 9:52
     */
    @PostMapping("createOrder")
    @ApiOperation(value = "业务员根据计划单帮助创建订单", notes = "配送方式1代表自提 2代表华鼎配送" + "jsonStr说明\n" + "Integer productId，" + "Integer price，" + "String grade，" + "Integer weight")
    public ServerResponse createOrder(@RequestBody String productListStr, Integer customerUserId, String opinion, Integer shippingId, Integer distCode, Integer transportId) throws Exception {

        return dealerService.createOrder(productListStr, opinion, customerUserId, shippingId, distCode, transportId);
    }


    @ApiOperation(value = "跟单员给资金管理员发送提醒事项", notes = "去token给后台调用")
    @PostMapping("remindFinance")
    public ServerResponse remindFinance(Integer userId, Long orderNo) throws Exception {

        return merchandiserService.remindFinance(userId, orderNo);
    }


    @ApiOperation(value = "nc系统插入结束之后推送信息给业务员")
    @GetMapping("ncFinishPush")
    public ServerResponse ncFinishPush(String orderNo, String subOrderNo, Integer userId) throws IOException, ParseException {

        return backService.ncFinishPush(orderNo, subOrderNo, userId);
    }

    @GetMapping("createTestData")
    @ApiOperation("创建测试数据")
    public ServerResponse createTestData(Integer customerUserId, Integer salesmanUserId, Integer merchUserId) throws InterruptedException {

        //
        List typeList = new ArrayList<>();
        typeList.add(1);
        typeList.add(2);
        typeList.add(3);
        typeList.add(4);
        return backService.createTestData(customerUserId, salesmanUserId, merchUserId, typeList);
    }

    /**
     * @return com.hmall.common.ServerResponse
     * @Date 2018/12/27 17:04
     * @Param [userId, packageNo]
     * @Description //TODO
     */
    @GetMapping("pushAuctionApply")
    @ApiOperation(value = "审核成功或者失败后第三方推送给用户", notes = "给后台调用的接口， 1表示成功 2表示失败")
    public ServerResponse pushAuctionApply(Integer userId, Long packageNo, @RequestParam(value = "type", defaultValue = "1") Integer type) throws IOException {

        return auctionService.pushAuctionApply(userId, packageNo, type);
    }


}
